
package ca.kooki.codesnippet.views;

import junit.framework.Assert;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.junit.Before;
import org.junit.Test;

import ca.kooki.codesnippet.Activator;
import ca.kooki.codesnippet.CodeSnippetRepository;
import ca.kooki.codesnippet.core.User;

public class NewUserViewHandlerTest extends SwtTest {

  private KeyEvent keyEvent;
  
  private Button buttonAdd;
  private Text inputName;
  private Label labelMessage;
  private NewUserViewHandler handler;
    
  
  @Before
  public void setUp() throws Exception {
    
    super.setUp();
    
    buttonAdd = new Button(parent,SWT.PUSH);
    inputName = new Text(parent,SWT.SINGLE);
    labelMessage = new Label(parent,SWT.LEFT);
    handler = new NewUserViewHandler(buttonAdd,inputName,labelMessage);
    
    Event event = new Event();
    event.widget = inputName;
    keyEvent = new KeyEvent(event);
    
    event = new Event();
    event.widget = inputName;
    
  }
  
  @Test
  public void EmptyTextDisableAdd()
  {
    inputName.setText("");
    handler.keyReleased(keyEvent);
    Assert.assertEquals(false,buttonAdd.isEnabled());
  }
  
  @Test
  public void NoTextInputDisableAdd()
  {
    inputName.setText(" ");
    handler.keyReleased(keyEvent);
    Assert.assertEquals(false,buttonAdd.isEnabled());
  }
  
  @Test
  public void HasTextEnableText()
  {
    inputName.setText("a");
    handler.keyReleased(keyEvent);
    Assert.assertEquals(true,buttonAdd.isEnabled());  
  }

  @Test
  public void ConstructorUpdatesAdd()
  {
    inputName.setText("");
    handler = new NewUserViewHandler(buttonAdd,inputName,labelMessage);
    Assert.assertEquals(false,buttonAdd.isEnabled());
    
    inputName.setText("b");
    handler = new NewUserViewHandler(buttonAdd,inputName,labelMessage);
    Assert.assertEquals(true,buttonAdd.isEnabled());
  }
  

  @Test
  public void DoNotEmptyUser()
  {
    inputName.setText(" ");
    handler = new NewUserViewHandler(buttonAdd,inputName,labelMessage);
    handler.addNewUser();
    
    Assert.assertEquals("Please provide a username", labelMessage.getText());
  }

  @Test
  public void UserAlreadyExists()
  {
    CodeSnippetRepository repository = Activator.getDefault().getRepository();
    repository.addUser(new User("abc"));
    
    inputName.setText("abc");
    handler = new NewUserViewHandler(buttonAdd,inputName,labelMessage);
    handler.addNewUser();
    
    Assert.assertEquals("Username already in use", labelMessage.getText());
  }

  
  @Test
  public void AddNewUser()
  {
    inputName.setText("abc");
    
    handler = new NewUserViewHandler(buttonAdd,inputName,labelMessage);
    handler.addNewUser();
    
    Assert.assertEquals("Success!", labelMessage.getText());
    
    CodeSnippetRepository repository = Activator.getDefault().getRepository();
    
    User[] allUsers = repository.getAllUsers();
    Assert.assertEquals(1, allUsers.length);
    Assert.assertEquals("abc", allUsers[0].getName());
    
  }
  
  @Test
  public void TrimAddedUser()
  {
    inputName.setText("  abc  ");
    handler = new NewUserViewHandler(buttonAdd,inputName,labelMessage);
    handler.addNewUser();
    
    Assert.assertEquals("Success!", labelMessage.getText());
    
    CodeSnippetRepository repository = Activator.getDefault().getRepository();

    Assert.assertEquals("abc", repository.getAllUsers()[0].getName());
  }
  
  
  @Test
  public void DoNotClearNewToDoInputAfterAdd()
  {
    handler = new NewUserViewHandler(buttonAdd,inputName,labelMessage);
    inputName.setText("aha");
    handler.keyReleased(keyEvent);
    Assert.assertEquals(true, buttonAdd.isEnabled());
    
    handler.addNewUser();
    
    Assert.assertEquals("aha",inputName.getText());
    Assert.assertEquals(true, buttonAdd.isEnabled());
  }
  
  @Test
  public void PressNumEnterAddNewUser()
  {
    keyEvent.keyCode = SWT.KEYPAD_CR;
    handler = new NewUserViewHandler(buttonAdd,inputName,labelMessage);
    inputName.setText("aha");
    handler.keyReleased(keyEvent);

    CodeSnippetRepository repository = Activator.getDefault().getRepository();
    Assert.assertEquals(1, repository.getAllUsers().length);
  }

  @Test
  public void PressEnterDoesNotAddNewItem()
  {
    keyEvent.keyCode = SWT.CR;
    handler = new NewUserViewHandler(buttonAdd,inputName,labelMessage);
    inputName.setText("aha");
    handler.keyReleased(keyEvent);

    CodeSnippetRepository repository = Activator.getDefault().getRepository();
    Assert.assertEquals(1, repository.getAllUsers().length);
  }

}
